package com.way;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 魏彪
 */
public class CompositeTest {

    public static void main(String[] args) {
        System.out.println("===============透明组合模式================");

        CourseComponent javaBase = new Course("Java入门课程",9299);

        CourseComponent ai = new Course("人工智能",5000);

        CourseComponent packageCourse = new CoursePackage("Java架构师课程",2);

        CourseComponent design = new Course("Java设计模式",1500);
        CourseComponent source = new Course("源码分析",2000);

        packageCourse.add(design);
        packageCourse.add(source);

        CourseComponent catalog = new CoursePackage("课程主目录",1);

        catalog.add(javaBase);
        catalog.add(ai);
        catalog.add(packageCourse);

        catalog.print();
    }
}

abstract class CourseComponent {
    public void add(CourseComponent component) {
        throw new UnsupportedOperationException("不支持添加操作");
    }

    public void remove(CourseComponent component) {
        throw new UnsupportedOperationException("不支持删除操作");
    }

    public String getName(CourseComponent component) {
        throw new UnsupportedOperationException("不支持获取名称操作");
    }

    public double getPrice() {
        throw new UnsupportedOperationException("不支持获取价格操作");
    }

    public void print() {
        throw new UnsupportedOperationException("不支持打印操作");
    }
}

class Course extends CourseComponent {
    private String name;

    private double price;

    public Course(String name, double price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String getName(CourseComponent component) {
        return this.name;
    }

    @Override
    public double getPrice() {
        return this.price;
    }

    @Override
    public void print() {
        System.out.println(name + "($ " + price + "元)");
    }
}

class CoursePackage extends CourseComponent {
    private List<CourseComponent> items = new ArrayList<>();

    private String name;

    private Integer level;

    public CoursePackage(String name, int level) {
        this.name = name;
        this.level = level;
    }

    @Override
    public void add(CourseComponent component) {
        items.add(component);
    }

    @Override
    public String getName(CourseComponent component) {
        return this.name;
    }

    @Override
    public void remove(CourseComponent component) {
        items.remove(component);
    }

    @Override
    public void print() {
        System.out.println(this.name);

        for (CourseComponent item : items) {
            // 控制格式显示
            if (this.level != null) {
                for (int i = 0; i < this.level; i++) {
                    // 打印空格控制格式
                    System.out.print(" ");
                }

                for (int i = 0; i < this.level; i++) {
                    if (i == 0) {
                        System.out.print("+");
                    }
                    System.out.print("-");
                }
            }
            item.print();
        }
    }
}
